package com.lagou.edu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author 应癫
 *
 * 事务管理器类：负责手动事务的开启、提交、回滚
 */
//@Component("transactionManager")
public class TransactionManager {

    private TransactionManager(){

    }
    private static TransactionManager transactionManager=new TransactionManager();
    public static TransactionManager getInstance(){
        return transactionManager;
    }

    private Connection conn;

    {
        try {
            conn = ConnectionUtils.getInstance().getCurrentThreadConn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        conn.setAutoCommit(false);
    }


    // 提交事务
    public void commit() throws SQLException {
        System.out.println("conn roback"+conn);
        conn.commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        System.out.println("conn roback"+TransactionManager.this.conn);
        conn.rollback();
    }
}
